package org.jaaksi.demo.layoutinflate;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewStub;
import android.widget.LinearLayout;

public class MyActivity extends Activity {

    private ViewStub view_stub;
    private View view;

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

//        view_stub = (ViewStub) findViewById(R.id.view_stub);

        LinearLayout container = (LinearLayout) findViewById(R.id.container);
//        for (int i = 0; i < 4; i++) {
//            // 写法一：如果设置为true，会自动添加到container中
////            inflate(R.layout.lib_text, container, true);
//
//            // 写法二：
        view = inflate(R.layout.lib_text, container, false);
        // 这里可以看到 params != null,如果调用两个参数的，即parent=null,那么会丢掉layoutparams属性
//        ViewGroup.LayoutParams params = view.getLayoutParams();
//        Log.i("fcy", "----------------- params=" + params);
//        container.addView(view);
//
//            // 写法三：（最简便）请查看源码 等价于方法二的两行代码
//            View.inflate(this, R.layout.lib_text, container);
//            // 下面这行代码等价于 LayoutInflater.from(this).inflate(R.layout.lib_text,null);会丢失view本身的LayoutParams属性
////            View view = View.inflate(this, R.layout.lib_text, null);
////            container.addView(view);
//            // 使用带有container的参数的方法，可以将跟视图的layoutParams属性赋给生成的view
//
//            // 下面的写法是不行的，TextView的与LayoutParams相关的属性都不能被生成，包括宽高、margin
//            // 相当于少了获取并设置LayoutParams,因为这里TextView没有父控件
////            View view = inflate(R.layout.lib_text, null);
//            // This method may return null if this View is not attached to a parent
////            LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view.getLayoutParams();//params=null
//            // 这个LayoutParams的类型是父控件的类型，这里父控件container是LinearLayout
////            LinearLayout.LayoutParams params =
////                    new LinearLayout.LayoutParams(200, 100);
////            params.setMargins(0, 0, 10, 0);
////            container.addView(view, params);
////            container.addView(view);
//
//        }
    }

    protected View inflate(int res, ViewGroup root) {
        return inflate(res, root, root != null);
    }

    protected View inflate(int res, ViewGroup root, boolean attachToRoot) {
        return getLayoutInflater().inflate(res, root, attachToRoot);
    }

    private boolean toggleFlag;

    public void onClick(View view) {
        ViewGroup.LayoutParams params = view.getLayoutParams();
        Log.i("fcy", "----------------- params=" + params);
    }
}
